# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_wx_basever=3.2

_realname=codeblocks
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=25.03
pkgrel=3
pkgdesc="A free C, C++ and Fortran IDE (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.codeblocks.org/'
msys2_repository_url="https://sourceforge.net/p/codeblocks/code/HEAD/tree/trunk/"
msys2_references=(
  'archlinux: codeblocks'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-hunspell"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
             "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-cb_headers"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "zip"
             "subversion")
optdepends=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra: needed by Clangd_Client plugin")
source=("https://downloads.sourceforge.net/project/codeblocks/Sources/${pkgver}/${_realname}_${pkgver}.tar.xz"
        '201-Build-Fix-compilation-with-MSYS2-GCC-15.1.patch')
sha256sums=('b0f6aa5908d336d7f41f9576b2418ac7d27efbc59282aa8c9171d88cea74049e'
            'def05b152e3a79db04135a9b23ddfd3373dcf813254eaeca7a21c0989dc7bf1a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}_${pkgver}"

  apply_patch_with_msg 201-Build-Fix-compilation-with-MSYS2-GCC-15.1.patch

  ./bootstrap
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export lt_cv_deplibs_check_method='pass_all'

  CXXFLAGS+=" -Wno-ignored-attributes" \
  ../${_realname}_${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --disable-pch \
    --with-contrib-plugins=all,-Valgrind

  make

  cp -R ${srcdir}/${_realname}_${pkgver}/src/tools/cbp2make/ "${srcdir}"/build-${MSYSTEM}/
  cd "${srcdir}"/build-${MSYSTEM}/cbp2make
  if check_option "debug" "n"; then
    make CC=${CC} CXX=${CXX} LD=${CXX} --jobs=1 -f cbp2make.cbp.mak.unix release
  else
    make CC=${CC} CXX=${CXX} LD=${CXX} --jobs=1 -f cbp2make.cbp.mak.unix debug
  fi
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  mv "${pkgdir}"${MINGW_PREFIX}/lib/codeblocks/bin/*.dll "${pkgdir}"${MINGW_PREFIX}/bin
  # Remove unneeded import libraries
  rm "${pkgdir}"${MINGW_PREFIX}/lib/codeblocks/plugins/*.dll.a

  install -Dm644 "${srcdir}"/${_realname}_${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  cd "${srcdir}"/build-${MSYSTEM}/cbp2make
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin/"
  if check_option "debug" "n"; then
    cp bin/Release/cbp2make.exe "${pkgdir}${MINGW_PREFIX}/bin/"
  else
    cp bin/Debug/cbp2make.exe "${pkgdir}${MINGW_PREFIX}/bin/"
  fi
}
